package com.zjl.springbatch.controller;

import com.zjl.springbatch.domain.Employee;
import com.zjl.springbatch.server.IEmployeeServer;
import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@RestController
public class JobController {
    @Autowired
    IEmployeeServer iEmployeeServer;
    @Value("${job.data.path}")
    private String path;

    @GetMapping("dataInit")//生成 csv文件   http://127.0.0.1:7777/dataInit
    public String dataInit() throws IOException {
        iEmployeeServer.dataInit();
        return "ok";

    }
    @RequestMapping("add")//添加一条 http://127.0.0.1:7777/add?id=3&name=李四2&age=55&sex=男
    public String add(Employee employee) throws IOException {
        iEmployeeServer.save(employee);
        return "ok";

    }
    @Autowired//@EnableBatchProcessing 这个注解帮你从 spring batch 的配置文件中加载了
    private JobLauncher jobLauncher;

    @Autowired
    private JobExplorer jobExplorer;

    @Autowired
    @Qualifier("csvToDBJob")//指定获取 那个名字的的Job
    private Job csvToDBJob;

    @GetMapping("csvToDB")//将csv文件读入临时表  http://127.0.0.1:7777/csvToDB
    public String csvToDB() throws Exception {
        File file = new File(path);
        if(!file.exists()){
            return "不存在csv文件:" + path;
        }
        //允许多次，每次操作前，需要清空临时表
        iEmployeeServer.truncateTemp();

        //启动作业  --可重复执行
        JobParametersBuilder jobParametersBuilder =
                new JobParametersBuilder(new JobParameters(), jobExplorer);

        JobParameters jobParameters = jobParametersBuilder
                .addDate("time", new Date())//封装识别参数，Job名称 + 参数，唯一决定一次批操作
                .getNextJobParameters(csvToDBJob)
                .toJobParameters();

        //运行  指定job
        JobExecution run = jobLauncher.run(csvToDBJob, jobParameters);


        return run.getId().toString();
    }
    @Autowired
    @Qualifier("dbToDBJob")//指定获取 那个名字的的Job
    private Job dbToDBJob;

    @GetMapping("dbToDB")// http://127.0.0.1:7777/dbToDB
    public String dbToDB() throws Exception {
        //允许多次，每次操作前，需要清空表
        iEmployeeServer.truncateAll();

        //启动作业  --可重复执行
        JobParametersBuilder jobParametersBuilder =
                new JobParametersBuilder(new JobParameters(), jobExplorer);

        JobParameters jobParameters = jobParametersBuilder
                .addDate("time", new Date())
                .getNextJobParameters(dbToDBJob)
                .toJobParameters();

        //运行  指定job
        JobExecution run = jobLauncher.run(dbToDBJob, jobParameters);


        return run.getId().toString();
    }
    @Autowired
    @Qualifier("partJob")//指定获取 那个名字的的Job
    private Job partJob;

    @Autowired//@EnableBatchProcessing 这个注解帮你从 spring batch 的配置文件中加载了
    @Qualifier("jobLauncher2")//指定获取 那个名字的的Job
    private JobLauncher jobLauncher2;

    @GetMapping("partJob")// http://127.0.0.1:7777/partJob
    public Long xxx() throws Exception {

        //启动作业  --可重复执行
        JobParametersBuilder jobParametersBuilder =
                new JobParametersBuilder(new JobParameters(), jobExplorer);

        JobParameters jobParameters = jobParametersBuilder
                .addDate("time", new Date())
                .getNextJobParameters(partJob)
                .toJobParameters();
        JobExecution run;
        System.out.println(jobLauncher2.getClass());

        System.out.println("-----------------");
        JobExecution run1 = jobLauncher2.run(partJob, jobParameters);
        System.out.println(run1.getJobId());

        //运行  指定job


        return  1L;
    }

}
